package com.emode.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.emode.base.BaseAction;
import com.emode.entity.SysPrivilege;
import com.emode.util.CommonUtils;
import com.emode.util.StringUtils;
import com.opensymphony.xwork2.ActionContext;

public class PriviAction extends BaseAction<SysPrivilege>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3107282040983440878L;
	private int parentId;
	private String ids;
	private String parentids;
	
	public String priviList(){
		List<SysPrivilege> list = privilegeservice.findAll();
		ActionContext.getContext().put("list", list);
		ActionContext.getContext().put("privi", list);
		//ActionContext.getContext().getValueStack().push(list);
		return "prilist";
	}
	
	/*
	 * 添加权限  
	 */
	
	public String addPrivi(){
		if(!("").equals(parentId)||parentId!=0){
			model.setParentid(privilegeservice.getById(parentId));
		}
		
		privilegeservice.save(model);
		return "toprilist";
	}
	//这里应该可以返回boolean类型的，当没有删除成功可以返回false
	public String deletePrivi(){
		if(!("").equals(parentId)||parentId!=0){
			System.out.println(model.getPid());
			privilegeservice.deleteByIds(model.getPid());;
		}
		return "toprilist";
		
	}
	
	public String updatePrivi() throws Exception{
		System.out.println(""+parentids);
		SysPrivilege p = new SysPrivilege();
		p.setPid(parentId);
		if(parentId==0){
			model.setParentid(null);;
		}else{
		model.setParentid(p);
		}
		privilegeservice.update(model);
		
		return "toprilist";
	}
	
	//菜单 测试	
	public String priviMenu(){
		List<SysPrivilege> list  = privilegeservice.findAll();
		String jsonStr = "[";
		Integer parentid = 0;
		for (SysPrivilege s : list) {
			if(s.getParentid()!=null){
				parentid = s.getParentid().getPid();
			}
			jsonStr +="{\"id\":\""+s.getPid() +"\",\"pId\":\""+ parentid +"\",\"name\":\"" +s.getName() +"\",\"url\":\""
			+ s.getUrl() + "\"},";
		}
		jsonStr = jsonStr.substring(0, jsonStr.length()-1)+"]";
		System.out.println(jsonStr.toString());
		CommonUtils.returnJson(jsonStr);
		return null;
		
	}
	public void priviMenu2(){
		List<SysPrivilege> list  = privilegeservice.findAll();
		List<Object> tree = new ArrayList<Object>(); 
		for(SysPrivilege p : list){
			Integer parentid = 0;
			Map<String,Object> data = new HashMap<String,Object>();
			if(p.getParentid()!=null){
				parentid = p.getParentid().getPid();
			}
			data.put("id", p.getPid());
			data.put("pId", parentid);
			data.put("name", p.getName());
			data.put("url", p.getUrl());
			tree.add(data);
			//data.put("", value)
			
		}
		JSONArray obj = new JSONArray(tree);
		//JSONObject obj = new JSONObject(tree);
		System.out.println(obj.toString());
		CommonUtils.returnJson(obj.toString());
	}
	public void updateTree(){
		System.out.println("pid="+model.getPid());
		System.out.println("parentids="+parentids);
		SysPrivilege privi = privilegeservice.getById(model.getPid());
		if(!StringUtils.isBlank(parentids)){
			System.out.println("更新权限");
			SysPrivilege parent = privilegeservice.getById(Integer.parseInt(parentids));
			parent.getChildren().add(privi);
			privilegeservice.update(parent);//这句话不执行
			CommonUtils.returnJson(model.getPid().toString());
		}else if(!StringUtils.isBlank(model.getName())){
			//p = privilegeservice.getById(model.getPid());
			privi.setName(model.getName());
			privilegeservice.update(privi);
			CommonUtils.returnJson(model.getPid().toString());
		}
		CommonUtils.returnJson("error");
	}
	
	public void treeData(){
		String data = privilegeservice.childNodeData(model,ids);
		CommonUtils.returnJson(data);
	}
	
	public void delTreeData(){
		System.out.println("pid"+model.getPid());
		SysPrivilege privi = privilegeservice.getById(model.getPid());
		privi.getGroup().remove(privi);
		privilegeservice.deleteByIds(model.getPid());
		CommonUtils.returnJson(model.getPid().toString());
		
	}
	public void addTreeData(){
		
		if(model.getName()!=null){
			privilegeservice.save(model);
			if(parentids!=null){
				SysPrivilege parent = privilegeservice.getById(Integer.parseInt(parentids));
				parent.getChildren().add(model);
				privilegeservice.update(parent);
			}
			CommonUtils.returnJson(model.getPid().toString());
		}else{
			CommonUtils.returnJson("error");
		}
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getParentids() {
		return parentids;
	}

	public void setParentids(String parentids) {
		this.parentids = parentids;
	}
	

}
